package com.ljx.itbbs.common.service.algorithm;

import java.util.Objects;

/**
 * @Author: LiJunXi
 * @Description: Trie树工具类
 * @Date: 2024-04-20  23:33
 */
public class TrieUtil {

    private static TrieNode root = new TrieNode();

    public static void insert(String word){
        TrieNode current = root;
        for(int i = 0; i < word.length(); i++){
            current = current.getChildren()
                    .computeIfAbsent(word.charAt(i), item -> new TrieNode());
        }
        current.setEnd(true);
    }

    public static boolean find(String word){
        TrieNode current = root;
        for(int i = 0; i < word.length(); i++){
            TrieNode node = current.getChildren().get(word.charAt(i));
            if (Objects.isNull(node)){
                return false;
            }
            current = node;
        }
        return current.isEnd();
    }

    public static void main(String[] args) {
        insert("草泥马");
        insert("傻逼");
        insert("你妹的");
        System.out.println(find("你妹的"));
    }
}
